﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrightIdeasSoftware;
using System.Windows.Forms;

namespace GDFlacTool.Common
{
    public class AlphaNumericComparer : IComparer
    {
        private OLVColumn _column;
        private SortOrder _sortOrder;
        private int compare;

        public AlphaNumericComparer(OLVColumn column, SortOrder sortOrder)
        {
            _column = column;
            _sortOrder = sortOrder;
        }

        public int Compare(object x, object y)
        {
            ListViewItem lx = (ListViewItem)x;
            ListViewItem ly = (ListViewItem)y;

            switch(_column.Index) // set in the constructor
            {
                case 0: // Filename
                case 1: // Artist
                case 2: // Album
                case 3: // Title
                case 4:
                case 5:
                case 6:
                case 7:
                case 8: // Tracknumber
                    compare = StringLogicalComparer.Compare(lx.SubItems[_column.Index].Text, ly.SubItems[_column.Index].Text);
                    break;
                default:
                    compare = StringLogicalComparer.Compare(lx.SubItems[_column.Index].Text, ly.SubItems[_column.Index].Text);
                    break;
            }

            if(_sortOrder == SortOrder.Ascending)
                return compare;
            return compare*-1;
        }
    }
}
